#include<vector>
#include<algorithm>
using namespace std;
class Solution
{
private:
    int cnt[100010];

public:
    int totalFruit(vector<int> &fruits)
    {
        int left = 0, right = 0, n = fruits.size();
        int ans = 0, sz = 0;
        for (; right < n; ++right)
        {
            int t = fruits[right];
            if (!cnt[t])
                ++sz;
            if (sz == 3)
            {
                ans = max(ans, right - left);
                // printf("before:%d\n",left);
                // cout << cnt[1] << endl;
                while (--cnt[fruits[left++]])
                    ;
                // printf("after:%d\n",left);
                --sz;
            }
            ++cnt[t];
        }
        return max(right - left, ans);
    }
};